package com.api.oa.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.api.oa.model.entity.YQWork;
import com.api.oa.services.YQWorkService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@RestController
@RequestMapping("/yq/yQWork")
public class YQWorkController extends BaseController {

    @Autowired
    private YQWorkService iYQWorkService;

    @RequestMapping(value = "/add", method = RequestMethod.POST, name = "添加护理任务轨迹点")
    public MyResult Add(@RequestBody YQWork entity) {
        try {
            return iYQWorkService.add(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "/upload/media", method = RequestMethod.POST, name = "护理媒体文件上次")
    public MyResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("taskId") Long taskId, @RequestParam("fileType") Integer type) throws IllegalStateException {
        try {
            String format = DateUtil.format(new Date(), "yyyy-MM");
            File root = new File(this.filesRootPath, format);
            File targetFile = new File(root, file.getOriginalFilename());
            FileUtil.touch(targetFile);
            file.transferTo(targetFile);
            YQWork yqWork = new YQWork();
            yqWork.setFileType(type);
            yqWork.setCreatedAt(DateUtil.date());
            yqWork.setTaskId(taskId);
            yqWork.setUrl(format + "/" + file.getOriginalFilename());
            iYQWorkService.add(yqWork);
        } catch (Exception e) {
            e.printStackTrace();
            return anomalous(e.getMessage());
        }
        return success("上传成功");
    }

    @RequestMapping(value = "/play/{id}", method = RequestMethod.GET, name = "媒体文件播放")
    public MyResult play(@PathVariable Long id, HttpServletResponse res) throws IllegalStateException {
        try {
            YQWork one = iYQWorkService.one(id);
            if (one == null) failed("未找到文件");
            //header(’Content-type: audio/amr’);
            res.addHeader("content-type", fileType(one.getFileType()));
            res.addHeader("Content-Disposition", "attachment;filename=" + new String(one.getUrl().getBytes(StandardCharsets.UTF_8), "ISO8859_1"));
            res.getOutputStream().write(FileUtil.readBytes(this.filesRootPath + "/" + one.getUrl()));
        } catch (Exception e) {
            e.printStackTrace();
            return anomalous(e.getMessage());
        }
        return success("上传成功");
    }

    private String fileType(int t) {
        switch (t) {
            case 0:
                return "audio/amr";
            case 1:
                return "video/mp4";
            case 2:
                return "image/*";
            default:
                return "";
        }
    }

    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST, name = "删除工作记录")
    public MyResult remove(@PathVariable Long id) {
        try {
            iYQWorkService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody YQWork entity) {
        try {
            return iYQWorkService.update(entity) == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET,name = "查询工作记录")
    public MyResult list(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) Long taskId) {
        try {
            return success(iYQWorkService.list(taskId, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET,name = "工作记录详情")
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null : iYQWorkService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


}
